package com.zto.tbschedule.console.zookeeper;

import org.I0Itec.zkclient.ZkClient;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.apache.zookeeper.server.auth.DigestAuthenticationProvider;

import java.security.NoSuchAlgorithmException;
import java.util.*;

public class ZKManager implements Watcher {
    private String connectString;
    private int sessionTimeout;
    private ZkClient zkClient;
    private Set<String> authStrs = new HashSet<String>();

    public List<ACL> getACL(String username,String password){
        List<ACL> acl = new ArrayList<ACL>();
        String authString =username
                + ":"+ password;
        try {
            acl.add(new ACL(ZooDefs.Perms.ALL, new Id("digest",
                    DigestAuthenticationProvider.generateDigest(authString))));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        acl.add(new ACL(ZooDefs.Perms.ALL, ZooDefs.Ids.ANYONE_ID_UNSAFE));
        return acl;
    }

    public ZKManager(String connectString, int sessionTimeout) {
        this.connectString = connectString;
        this.sessionTimeout = sessionTimeout;
        zkClient = new ZkClient(connectString, sessionTimeout,5000,FastJsonZKSerializer.getInstance());
    }

    public List<String> getChildren(String path) {
        return zkClient.getChildren(path);
    }

    public String getData(String path) {
        String data = zkClient.readData(path);
        return data;
    }
    public String createData(String path,String data,String userName,String password,CreateMode mode){
        return zkClient.create(path,data,getACL("admin","admin"),mode);
    }

    public void uppData(String path,String data,String authStr){
        if(!authStrs.contains(authStr)){
            byte[]  auth = authStr.getBytes();
            zkClient.addAuthInfo("digest", auth);
        }
        zkClient.writeData(path,data);
    }
    public boolean exists(String path){
        return zkClient.exists(path);
    }
    public void delete(String path,String authStr){
        if(!authStrs.contains(authStr)){
            byte[]  auth = authStr.getBytes();
            zkClient.addAuthInfo("digest", auth);
        }
        String[] list = getTree(path);
        for(int i= list.length -1;i>=0; i--){
            zkClient.delete(list[i]);
        }
    }

    public String[] getTree(String path){
        if(!zkClient.exists(path)){
            return new String[0];
        }
        List<String> dealList = new ArrayList<String>();
        dealList.add(path);
        int index =0;
        while(index < dealList.size()){
            String tempPath = dealList.get(index);
            List<String> children = this.getChildren(tempPath);
            if(tempPath.equalsIgnoreCase("/") == false){
                tempPath = tempPath +"/";
            }
            Collections.sort(children);
            for(int i = children.size() -1;i>=0;i--){
                dealList.add(index+1, tempPath + children.get(i));
            }
            index++;
        }
        return (String[])dealList.toArray(new String[0]);
    }


    public void process(WatchedEvent watchedEvent) {

    }
}
